package com.luozhili.rabbitmqlearningdemo.subscribe_demo;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.nio.charset.StandardCharsets;

public class Send {
    private final static String EXCHANGE_NAME = "fanout_learn_exchange_1";

    public static void main(String[] argv) throws Exception {

            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("192.168.31.226");
            factory.setUsername("zhangsan");
            factory.setPassword("123456");
            factory.setVirtualHost("/shopping");


            try (Connection connection = factory.newConnection() ;
                 Channel channel = connection.createChannel()
                  ) {
                //声明路由器（没有则会创建）  路由方式为：fanout（所有订阅者都接收）
                channel.exchangeDeclare(EXCHANGE_NAME,"fanout");

                String message = "hello user , this is a  message from subscribe";

                //发送到路由器中
                channel.basicPublish( EXCHANGE_NAME ,"", null, message.getBytes(StandardCharsets.UTF_8));
                System.out.println(" [x] 已发送 '" + message + "'");


            }


    }
}
